<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
use api\wxapp\model\Clerk;
use api\wxapp\model\User;
use api\wxapp\model\Shop;
use api\wxapp\model\Order;
use api\wxapp\model\Balance;
use api\wxapp\model\Cash;
use api\wxapp\model\ShopGoods;
use api\wxapp\model\Goods;
use api\wxapp\model\OrderList;
use api\wxapp\controller\WxRcodeController;
class ClerkController extends ApiBaseController
{
    public function initialize()
    {
        parent::initialize();
        $params = $this->getParam();
        $this->checkuid($params);
        $find = Clerk::where('uid',$params['uid'])->find();
        if(!$find){
            $this->error('没有权限!!!!!');
        }
        if($find['status'] !== 2){
            $this->error('未审核通过!!!!!');
        }
    }
    public function index()
    {
        $params = $this->getParam();
        //查询当前订单状态
        $this->checkuid($params);
        $this->checkshopid($params);
        !isset($params['username']) ? $this->error('参数缺失!!!!') : '';
        !isset($params['sex']) ? $this->error('参数缺失!!!!') : '';
        !isset($params['birthday']) ? $this->error('参数缺失!!!!') : '';
        $params['addtime'] = $this->returnDateTime();
        $find = Clerk::where('uid',$params['uid'])->find();
        if($find){
            $this->error('此用户已经是店员!',false);
        }
        $res = Clerk::insert($params);
        if($res){
            $this->success('申请成功!',true);
        }else{
            $this->error('申请失败!',false);
        }
    }
    //店员--个人中心
    public function info()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        $user = User::get($params['uid']);
        $info['nickname'] = $user['nickname'];
        $info['img'] = $user['img'];
        $clerk = Clerk::where('uid',$params['uid'])->find();
        $shop = Shop::get($clerk['shop_id']);
        $info['shopname'] = $shop['name'];
        $info['shop_id'] = $shop['id'];
        $where[] = ['uid','eq',$params['uid']];
        $where[] = ['type','eq',1];
        $info['total'] = Balance::where($where)->SUM('balance');
        $where[] = ['addtime','like','%'.date('Y-m-d',time()).'%'];
        $info['today'] = Balance::where($where)->SUM('balance');
        $info['balance'] = $user['balance'];
        $this->success('请求成功!',$info);
    }
    //店员--我的余额
    public function my()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        $where = array();
        $where[] = ['uid','eq',$params['uid']];
        if(isset($params['type'])){
            $where[] = ['type','eq',$params['type']];
        }
        $user = User::get($params['uid']);
        $list = Balance::where($where)->select();
        $this->success('请求成功!',['info'=>$user,'list'=>$list]);
    }

    //提现申请
    public function cash()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        switch($this->getMethod()){
            case 'GET':
                $list = Cash::where('uid',$params['uid'])->select();
                $this->success('请求成功!',$list);
            break;
            case 'POST':
                if(!isset($params['price'])){
                    $this->error('参数缺失!',false);
                }
                $params['addtime'] = $this->returnDateTime();
                $res = Cash::insert($params);
                if($res){
                    $this->success('请求成功!',true);
                }else{
                    $this->error('请求失败');
                }
            break;
        }
    }
    ///二维码
    public function rcode()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        //查询当前店员是否生成二维码
        $info = Clerk::where('uid',$params['uid'])->find();
        if($info['rcode']){
            $this->success('请求成功!',$info['rcode']);
        }else{
            $rcode = new WxRcodeController;
            $data = json_encode(array("path"=>"pages/index/index?uid=".$params['uid']."&shop_id=".$info['shop_id']."&type=0","width"=> 50));
            $res = $rcode->index($data,$params['uid']);
            Clerk::where('id',$info['id'])->update(['rcode'=>$res]);
            $this->success('请求成功!',$res);
        }
    }
    //我的用户
    public function myuser()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        $where[] = ['puid','eq',$params['uid']];
        if(isset($params['stime'])){
            $where[] = ['addtime','egt',$params['stime']];
            $where[] = ['addtime','elt',$params['etime']];
        }
        $list = User::where($where)->select();
        $this->success('请求成功!',$list);
    }
    //销售情况
    public function sale()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        //查询当前店员属于哪一个哪一个店铺
        $shop_id = User::where('id',$params['uid'])->value('shop_id');
        $order = Order::where('shop_id',$shop_id)->column('id');
        //匹配订单商品详情表的 商品 是店售 还是直邮 //默认是店售
        $where = array();
        $map[] = ['order_id','in',$order];
        $where[] = ['order_id','in',$order];
        $goods_id = ShopGoods::where('shop_id',$shop_id)->column('goods_id');
        $count = OrderList::where($map)->SUM('num');
        $salemoney = Order::where('shop_id',$shop_id)->SUM('pay_price');

        $salemoney = (string)$salemoney;
        $arr['shop_id'] = $shop_id;
        $arr['order_id'] = $order;
        //直邮的
        if(isset($params['type']) && $params['type'] == 2){
            $where[] = ['goods_id','notin',$order];
            $arr['type'] = $params['type'];
        }else{
            $where[] = ['goods_id','in',$goods_id];
            $arr['type'] = 0;
        }
        $list = OrderList::where($where)->field('goods_id,price')->select()
        ->each(function($item,$key) use ($arr){
            $where1[] = ['goods_id','eq',$item['goods_id']];
            $where1[] = ['order_id','in',$arr['order_id']];
            $item['count'] = OrderList::where($where1)->SUM('num');
            $goods = Goods::get($item['goods_id']);
            $item['img'] = $goods['img'];
            $item['name'] = $goods['name'];
            
        });
        $data['count'] = $count;
        $data['salemoney'] = $salemoney;
        $data['list'] = $list;
        $this->success('请求成功!',$data);
    }
    //核销
    public function write()
    {
        $params = $this->getParam();
        switch($this->getMethod()){
            case 'GET':
                $this->checkuid($params);
                $info = Clerk::where('uid',$params['uid'])->find();
                $where = array();
                $where[] = ['t1.shop_id','eq',$info['shop_id']];
                $where[] = ['t2.status','eq',2];
                //查询当前店铺的所有订单 已核销
                $list = Order::alias('t1')
                    ->join('app_goods_order_list t2','t2.order_id = t1.id')
                    ->where($where)
                    ->field('t1.ordernum,t1.status,t1.remark,t1.id,t2.goods_id,t2.num,t2.price,t1.score')
                    ->select()
                    ->each(function($item,$key){
                        $goods = Goods::get($item['goods_id']);
                        $item['img'] = $goods['img'];
                        $item['name'] = $goods['name'];
                    });
                $this->success('请求成功!',$list);
            break;
            case 'POST':
                !isset($params['order_id']) ? $this->error('参数缺失!!!!') : '';
                !isset($params['goods_id']) ? $this->error('参数缺失!!!!') : '';
                $where = array();
                $where[] = ['order_id','eq',$params['order_id']];
                $where[] = ['goods_id','eq',$params['goods_id']];
                $res = OrderList::where($where)->update(array('status'=>2));
                if($res){
                    $this->success('核销成功!');
                }else{
                    $this->error('核销失败');
                }
            break;
            case 'DELETE':
                !isset($params['order_id']) ? $this->error('参数缺失!!!!') : '';
                //删除订单 及删除订单对应的商品
                Db::startTrans();
                try{
                    $res1 = Order::destroy($params['order_id']);
                    if(!$res1){
                        throw new \Exception('订单表删除失败');
                    }
                    $res2 = OrderList::where('order_id',$params['order_id'])->delete();
                    if(!$res2){
                        throw new \Exception('订单商品表删除失败');
                    }
                    Db::commit(); //提交事务
                }catch(\Exception $e){
                    Db::rollback(); //回滚事务
                    $this->error($e->getMessage());
                }
                $this->success('删除成功!');
            break;
        }
    }
}
